package org.qs.webservice.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.qs.webservice.dao.user.UsBookRecentMapper;
import org.qs.webservice.domain.UsBookRecent;
import org.qs.webservice.domain.UsBookShelf;
import org.qs.webservice.service.BookRecentService;
import org.qs.webservice.service.IreadMemcacheService;
import org.qs.webservice.util.DateUtil;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.qs.webservice.vo.Content;
import org.qs.webservice.vo.PageData;
import org.springframework.stereotype.Service;

@Service
public class BookRecentServiceImpl implements BookRecentService {
	@Resource
	private IreadMemcacheService ireadMemcacheService;
	@Resource
	private UsBookRecentMapper usBookRecentMapper;
	@Override
	public String getRecents(String token, String device_identifier, int pages,
			int contents,String order) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		String user_id  = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason", "用户未登录");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		if(StringUtils.isNullOrEmpty(order)){
			order="desc";
		}
		if(!"asc".equals(order.toLowerCase())){
			order="desc";
		}
		PageData<Content> pageData = new PageData<Content>();
		pageData.setCode(200);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		int index=(pages-1)*contents;
		List<Content>contentList = new ArrayList<Content>();
		List<UsBookRecent> recents = usBookRecentMapper.list(index, contents, Long.valueOf(user_id),order);
		for(UsBookRecent usBookRecent:recents){
			Content content = new Content();
			content.setMark_id(usBookRecent.getMark_id()+"");
			content.setContent_id(usBookRecent.getBook_id());
			content.setContent_name(usBookRecent.getBook_name());
			if(usBookRecent.getChapter_id()==0){
				content.setChapter_id(usBookRecent.getFirst_chap_id());
			}else
				content.setChapter_id(usBookRecent.getChapter_id());
			content.setChapter_name(usBookRecent.getChapter_name());
			content.setAuthor_name(usBookRecent.getAuthor_name());
			content.setAuthor_id(usBookRecent.getAuthor_id()+"");
			content.setChapter_offset(usBookRecent.getChapter_offset());
			content.setMark_time(DateUtil.getString(usBookRecent.getModify_time(), "yyyy-MM-dd HH:mm:ss"));
			content.setImage_url(usBookRecent.getBook_cover());
			contentList.add(content);
		}
		pageData.setContent(contentList);
		pageData.setTotal(usBookRecentMapper.listNum(Long.valueOf(user_id)));
		return IReadUtils.beanToJson(pageData);
	}
	@Override
	public String addRecents(String param, String token,
			String device_identifier) {
		//List<UsBookShelf> usBookShelfs = Collection  
		/*if(StringUtils.isNullOrEmpty(user_id)){
		UsBookShelf usBookShelf = usBookShelfMapper.getShelfByBookIdAndUserId(book_id,Long.valueOf(user_id));
		if(usBookShelf!=null){
			usBookShelf.setChap_id(chapter_id);
		}
		UpdateTask updateTask = new UpdateTask(1, usBookRecent);
		updateTask.run();
	}*/
		return "";
	}
	
}
